<?php

class ContentAction extends PublicAction
{
    protected $Config;
    protected $SysConfig;
    protected $Categorys;
    protected $Mod = array();
    protected $Model;
    protected $Lang;

    //获取单页面信息
    function page()
    {
        //栏目id
        $catid = I('catid', 0, 'intval');

        if (!empty($catid) && array_key_exists($catid, $this->Categorys)) {

            $data['data'] = M('Page')->find($catid);

            $data['code'] = 200;
            $data['message'] = 'success';


        } else {
            $data['message'] = 'error';
        }

        $this->ajaxReturn($data);
    }

    function getList()
    {
        //栏目id
        $catid = I('catid', 0, 'intval');

        if (!empty($catid) && array_key_exists($catid, $this->Categorys)) {
            $cat = $this->Categorys[$catid];
            $modelname = $cat['model'];

            $condition = array();
            $condition['status'] = 1;
            $condition['create_time'] = array('ELT', time());

            if (!empty($_REQUEST['type']) && is_array($_REQUEST['type'])) {
                foreach ($_REQUEST['type'] as $key => $value) {
                    if (!empty($value)) {
                        $condition[$key] = $value;
                    }
                }
            }

            $keyword = I('keyword', '');
            if ($keyword) {
                $condition['title'] = array('like', "%$keyword%");
            }
            $data = array();

            $model_db = M($modelname);

            if ($cat['child']) {
                $condition['catid'] = array('in', $cat['arrchildid']);
            } else {
                $condition['catid'] = $catid;
            }

            $count = $model_db->where($condition)->count();

            $p = I('p', 0);

            if ($count) {
                import("@.ORG.Page");
                $listRows = I('listrows', C('PAGE_LISTROWS'));
                $totalPage = ceil($count / $listRows);
                if ($p > $totalPage) {
                    $data['msg'] = 'error';
                } else {
                    $page = new Page($count, $listRows);

                    $page->urlrule = geturl($cat, '');

                    $field = $this->Model[$this->Mod[$modelname]]['listfields'];
                    $field = $field ? $field : '*';

                    $list = $model_db->field($field)->where($condition)->order('listorder desc,id desc')->limit($page->firstRow . ',' . $page->listRows)->select();

                    foreach ($list as $key => $val) {
                        $list[$key]['create_time'] = date('Y-m-d', $val['create_time']);
                        if (!empty($_REQUEST['type']) && is_array($_REQUEST['type'])) {
                            foreach ($_REQUEST['type'] as $typeField => $value) {
                                $list[$key][$typeField . '_title'] = getTypeData($val[$typeField], 'name');
                            }
                        }
                    }

                    $data['code'] = 200;
                    $data['message'] = 'success';
                    $data['data']['list'] = $list;
                    $data['data']['amount'] = $count;
                    $data['data']['totalPage'] = ceil($count / $listRows);
                }
            } else {
                $data['message'] = 'error';
            }

            $this->ajaxReturn($data);
        }
    }

    function getInfo()
    {
        //栏目id
        $catid = I('get.catid', 0, 'intval');

        if (empty($catid)) {
            $catdir = I('get.catdir');

            foreach ($this->Categorys as $key => $val) {
                if (!empty($val['catdir']) && $val['catdir'] == $catdir) {
                    $catid = $val['id'];
                }
            }
        }

        $cat = $this->Categorys[$catid];
        $modelname = $cat['model'];

        $id = I('get.id', 0, 'intval');
        $p = I('p', 1, 'intval');

        $model_db = M($modelname);;
        $data = $model_db->find($id);
        if (empty($data)) {
            header("HTTP/1.0 404 Not Found");
            $this->display('./public/404.html');
            exit;
        }

        $model_db->where("id=" . $id)->setInc('hits'); //添加点击次数

        $fields = getCache('Field_' . $cat['modelid']);

        foreach ($data as $key => $c_d) {
            if (!empty($fields[$key])) {
                $setup = !empty($fields[$key]['setup']) ? json_decode($fields[$key]['setup'], true) : '';

                if ($fields[$key]['type'] == 'relation') {
                    //关联信息
                    if (!empty($data[$key])) {
                        $temp_modelname = getModel($setup['modelid'], 'tablename');
                        $data[$key] = json_decode($data[$key], true);
                        $data[$key] = M($temp_modelname)->field('url,title,thumb')->where(array('id' => array('in', $data[$key])))->select();
                    }
                }

                if (!empty($setup['fieldtype']) && $setup['fieldtype'] == 'varchar' && $fields[$key]['type'] != 'text') {
                    $data[$key . '_old_val'] = $data[$key];
                    $data[$key] = fieldoption($fields[$key], $data[$key]);
                } elseif ($fields[$key]['type'] == 'images' || $fields[$key]['type'] == 'files') {
                    if (!empty($data[$key])) {
                        $data[$key] = json_decode($data[$key], true);
                    }
                }
            }
        }

        //手动分页
        $CONTENT_POS = strpos($data['content'], '[page]');
        if ($CONTENT_POS !== false) {

            $urlrule = geturl($cat, $data);
            $urlrule = str_replace('%7B%24page%7D', '{$page}', $urlrule);
            $contents = array_filter(explode('[page]', $data['content']));
            $pagenumber = count($contents);

            $pageurls = array();
            for ($i = 1; $i <= $pagenumber; $i++) {
                $pageurls[$i] = str_replace('{$page}', $i, $urlrule);
            }

            $assign['pages'] = content_pages($pagenumber, $p, $pageurls);
            //判断[page]出现的位置是否在文章开始
            if ($CONTENT_POS < 7) {
                $data['content'] = $contents[$p];
            } else {
                $data['content'] = $contents[$p - 1];
            }
        }

        $this->ajaxReturn($data);
    }

    public function area()
    {
        $module = M('area');
        $id = intval($_REQUEST['id']);
        $level = intval($_REQUEST['level']);
        $provinceid = intval($_REQUEST['provinceid']);
        $cityid = intval($_REQUEST['cityid']);
        $areaid = intval($_REQUEST['areaid']);

        $province_str = '<option value="0">请选择省份...</option>';
        $city_str = '<option value="0">请选择城市...</option>';
        $area_str = '<option value="0">请选择区域...</option>';
        $str = '';
        $r = $module->where("parentid='" . $id . "'")->select();

        foreach ($r as $key => $pro) {
            $selected = ($pro['id'] == $provinceid) ? ' selected="selected" ' : '';
            $str .= '<option value="' . $pro['id'] . '"' . $selected . '>' . $pro['name'] . '</option>';
        }
        if ($level == 0) {

            $province_str .= $str;
        } elseif ($level == 1) {
            $city_str .= $str;
        } elseif ($level == 2) {
            $area_str .= $str;
        }
        $str = '';
        if ($provinceid) {

            $rr = $module->where("parentid=" . $provinceid)->select();
            foreach ($rr as $key => $pro) {
                $selected = ($pro['id'] == $cityid) ? ' selected="selected" ' : '';
                $str .= '<option value="' . $pro['id'] . '"' . $selected . '>' . $pro['name'] . '</option>';
            }
            $city_str .= $str;
        }

        $str = '';
        if ($cityid) {
            $rrr = $module->where("parentid=" . $cityid)->select();
            foreach ($rrr as $key => $pro) {
                $selected = ($pro['id'] == $areaid) ? ' selected="selected" ' : '';
                $str .= '<option value="' . $pro['id'] . '"' . $selected . '>' . $pro['name'] . '</option>';
            }
            $area_str .= $str;
        }

        $res = array();
        $res['data'] = $rs ? 1 : 0;
        $res['province'] = $province_str;
        $res['city'] = $city_str;
        $res['area'] = $area_str;

        echo json_encode($res);
    }
}